<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
    xmlns:jsf="http://xmlns.jcp.org/jsf"
    xmlns:pt="http://java.sun.com/jsf/passthrough"
    xmlns:cc="http://java.sun.com/jsf/composite"
    xmlns:o="http://omnifaces.org/ui"
    xmlns:of="http://omnifaces.org/functions"
    xmlns:iqbs="http://xmlns.jcp.org/jsf/composite/iqbs">
    
    <ui:remove>
    <!--
    If you include this fragment, you must also include file-edit-popup-fragment.xhtml
    
    This can be used for a single, specific file (passing "fileMetadata") or a group of selected files.
    
    Parameters:
    dataserVersion - the datasetversion object to associate with this view
    fileMetadata - for single file, the fileMetadata object of that file
    fileMetadataForAction - used by DatasetPage popups to identify single file (also clears for selected file)
    configureTools - for single file, list of configureTools for the file
    bean - the named value of the backing bean for the below method(s), also used by isFilePg param
    unrestrictFileAction - name of the method on the above bean to call for unrestrict (method must take a boolean)    
    refreshTagsPopoupAction - for selected files, name of method on the above bean to refresh tags popup
    -->            
    </ui:remove>
    
    <ui:param name="isFilePg" value="#{of:isInstance('edu.harvard.iq.dataverse.FilePage', bean)}"/>


    <!-- Edit Options -->
    <li>
        <p:commandLink onclick="if (!(#{fileMetadata!=null} || testFilesSelected()))
                    return false;"                        
                       oncomplete="if(#{fileMetadata!=null}) window.location.assign('/editdatafiles.xhtml?selectedFileIds=#{fileMetadata.dataFile.id}&#38;datasetId=#{fileMetadata.datasetVersion.dataset.id}#{isFilePg?'&#38;referrer=FILE':''}&#38;version=#{fileMetadata.datasetVersion.version}')"
                       action="#{bean.editFileMetadata()}">
            <h:outputText value="#{bundle['file.metadata']}"/>
        </p:commandLink>
    </li>

    <ui:fragment rendered="#{!bean.hasPublicStore}">
        <ui:fragment rendered="#{fileMetadata == null or !fileMetadata.restricted}">
            <li>
                <p:commandLink  onclick="if (!(#{fileMetadata!=null} || testFilesSelected()))
                                            return false;" 
                                update="@([id$=accessPopup])"            
                                oncomplete="PF('accessPopup').show();">
                    <f:setPropertyActionListener target="#{fileMetadataForAction}" value="#{fileMetadata}" />
                    <f:setPropertyActionListener target="#{bean.termsOfAccess}" value="#{datasetVersion.termsOfUseAndAccess.termsOfAccess}" />
                    <f:setPropertyActionListener target="#{bean.fileAccessRequest}" value="#{datasetVersion.termsOfUseAndAccess.fileAccessRequest}" />                     
                    <h:outputText value="#{bundle['file.restrict']}"/>                                                                
                </p:commandLink>
            </li>
        </ui:fragment>
        <ui:fragment rendered="#{fileMetadata == null or fileMetadata.restricted}">
            <li>
                <p:commandLink  
                    onclick="if (!(#{fileMetadata!=null} || testFilesSelected()))
                                return false;" 
                    action="#{bean[unrestrictFileAction](false)}">
                    <f:setPropertyActionListener target="#{fileMetadataForAction}" value="#{fileMetadata}" />
                    <h:outputText value="#{bundle['file.unrestrict']}"/>
                </p:commandLink>
            </li>         
        </ui:fragment>   
    </ui:fragment>

    <!-- Replace is only available for single file actions-->
    <ui:fragment rendered="#{fileMetadata!=null}">                                   
        <li>
            <h:outputLink value="/editdatafiles.xhtml?mode=REPLACE#{isFilePg?'&#38;referrer=FILE':''}&#38;datasetId=#{fileMetadata.datasetVersion.dataset.id}&#38;fid=#{fileMetadata.dataFile.id}&#38;version=#{fileMetadata.datasetVersion.version}">
                <h:outputText value="#{bundle['file.replace']}"/>
            </h:outputLink>
        </li>
    </ui:fragment>
    
    <!-- Single file uningest/reingest -->
    <ui:fragment rendered="#{isFilePg and (dataverseSession.user.superuser and FilePage.fileMetadata.dataFile.isTabularData()) or (FilePage.fileMetadata.dataFile.isIngestProblem() and FilePage.canPublishDataset())}">
        <li>
            <p:commandLink update="@form,:messagePanel" action="#{FilePage.uningestFile()}">
                <h:outputText value="#{bundle['file.uningest']}"/>
            </p:commandLink>
        </li>
    </ui:fragment>
    <ui:fragment rendered="#{isFilePg and dataverseSession.user.superuser and FilePage.isIngestable()}">
        <li>
            <p:commandLink update="@form,:messagePanel" actionListener="#{FilePage.ingestFile()}">
                <h:outputText value="#{bundle['file.ingest']}"/>
            </p:commandLink>
        </li>
    </ui:fragment>

    <!-- TO-DO #3488 - ADD FILE TAGS FOR FILE PG AND SINGLE FILE-->    
    <ui:fragment rendered="#{fileMetadata==null}">    
        <li>
            <p:commandLink      update="@([id$=fileTagsPopup])" 
                                onclick="if (!testTermsOfAccess() || !(#{fileMetadata!=null} || testFilesSelected()))
                                            return false;"                                 
                                action="#{bean[refreshTagsPopoupAction]()}">            
                <h:outputText value="#{bundle['file.tags']}"/>
            </p:commandLink>    
        </li>  
    </ui:fragment> 
    
    <ui:fragment rendered="#{settingsWrapper.embargoAllowed}">
        <li>
            <p:commandLink      update="@([id$=fileEmbargoPopup])" 
                                onclick="if (!(#{fileMetadata!=null} || testFilesSelected()))
                                            return false;" 
                                oncomplete="PF('fileEmbargoPopup').show();">
                                <f:setPropertyActionListener target="#{fileMetadataForAction}" value="#{fileMetadata}" />
                <h:outputText value="#{bundle['file.embargo']}"/>
            </p:commandLink> 
        </li>
    </ui:fragment>

    <ui:fragment rendered="#{settingsWrapper.retentionAllowed}">
        <li>
            <p:commandLink      update="@([id$=fileRetentionPopup])"
                                onclick="if (!(#{fileMetadata!=null} || testFilesSelected()))
                                            return false;"
                                oncomplete="PF('fileRetentionPopup').show();">
                <f:setPropertyActionListener target="#{fileMetadataForAction}" value="#{fileMetadata}" />
                <h:outputText value="#{bundle['file.retention']}"/>
            </p:commandLink>
        </li>
    </ui:fragment>

    <!-- TO-DO #7342 - ADD PROVENANCE FOR SINGLE FILE-->    
    <ui:fragment rendered="#{isFilePg and systemConfig.provCollectionEnabled}">
        <li>
            <p:commandLink
                update="@([id$=editProvenancePopup])"
                oncomplete="PF('editProvenancePopup').show();">
                <f:actionListener binding="#{provPopupFragmentBean.updatePopupState(fileMetadata, true)}" /> <!--clearProvenanceUpdates() -->
                #{bundle['file.provenance']} 
            </p:commandLink>
        </li> 
    </ui:fragment>     

    <li>
        <p:commandLink title="#{bundle['file.delete']}" update="@([id$=deleteFileConfirm])" 
                       onclick="if (!(#{fileMetadata!=null} || testFilesSelected()))
                                   return false;" 
                       oncomplete="PF('deleteFileConfirmation').show();">
            <f:setPropertyActionListener target="#{fileMetadataForAction}" value="#{fileMetadata}" />                
            <h:outputText value= "#{bundle['file.delete']}"/>
        </p:commandLink>
    </li>


    <!---Modular configure-->
    <ui:fragment rendered="#{configureTools.size() > 0}">		
        <ui:include src="file-configure-dropdown-fragment.xhtml">
            <ui:param name="configureTools" value="#{configureTools}"/>
            <ui:param name="fileId" value="#{fileMetadata.dataFile.id}"/>
            <ui:param name="fileMetadata" value="#{fileMetadata}"/>				
        </ui:include>		
    </ui:fragment>
  
</ui:composition>
